<template>
  <div class="chat-container">
    <div class="chat-header">
      <h1>DeepSeek 聊天助手</h1>
    </div>

    <!-- 新增：快捷问题区域 -->
    <div class="quick-questions">
      <button
          v-for="(question, index) in quickQuestions"
          :key="index"
          @click="handleQuickQuestion(question)"
          class="quick-btn"
      >
        {{ question }}
      </button>
    </div>

    <div class="chat-messages" ref="messagesContainer">
      <div
          v-for="(msg, index) in messages"
          :key="index"
          :class="['message', msg.role]"
      >
        <div class="avatar">
          {{ msg.role === 'user' ? '我' : 'AI' }}
        </div>
        <div class="content">{{ msg.content }}</div>
      </div>
      <div v-if="loading" class="loading">正在思考中...</div>
    </div>

    <div class="chat-input">
      <textarea
          v-model="inputMessage"
          placeholder="请输入消息..."
          @keydown.enter.exact="sendMessage"
          @keydown.enter.shift="() => {}"
      ></textarea>
      <button @click="sendMessage" :disabled="!inputMessage.trim() || loading">
        发送
      </button>
    </div>
  </div>
</template>

<script>
import deepSeekApi from '../api/deepSeek_api.js'

export default {
  data() {
    return {
      messages: [],
      inputMessage: '',
      loading: false,
      // 新增：定义快捷问题列表
      quickQuestions: [
        '羽毛球装备选购指南',
        '羽毛球运动注意事项',
        '羽毛球基础发球技巧',
        '如何提升羽毛球反手能力'
      ]
    }
  },
  methods: {
    async sendMessage() {
      const message = this.inputMessage.trim()
      if (!message || this.loading) return

      this.messages.push({ role: 'user', content: message })
      this.inputMessage = ''
      this.loading = true

      try {
        const response = await deepSeekApi.chat(message)
        const content = response?.choices?.[0]?.message?.content || '未获取到有效回复'
        this.messages.push({ role: 'assistant', content: content })
      } catch (error) {
        this.messages.push({
          role: 'assistant',
          content: `发生错误: ${error.message || '未知错误'}`
        })
        console.error('请求失败:', error)
      } finally {
        this.loading = false
        this.scrollToBottom()
      }
    },

    // 新增：处理快捷问题点击
    handleQuickQuestion(question) {
      // 将问题填入输入框并自动发送
      this.inputMessage = question
      this.sendMessage()
    },

    scrollToBottom() {
      this.$nextTick(() => {
        const container = this.$refs.messagesContainer
        container.scrollTop = container.scrollHeight
      })
    }
  },
  mounted() {
    this.messages.push({
      role: 'assistant',
      content: '你好！我是DeepSeek AI助手，有什么可以帮助你的吗？'
    })
  }
}
</script>

<style scoped>
/* 原有样式保持不变 */
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
}

.chat-header {
  background-color: #f5f5f5;
  padding: 16px;
  text-align: center;
  border-bottom: 1px solid #e0e0e0;
}

/* 新增：快捷问题样式 */
.quick-questions {
  padding: 12px 16px;
  background-color: #fff;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.quick-btn {
  padding: 6px 12px;
  background-color: #f0f7ff;
  color: #007bff;
  border: 1px solid #e1ecf4;
  border-radius: 16px;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
  transition: all 0.2s;
}

.quick-btn:hover {
  background-color: #e1ecf4;
  border-color: #b3d3ea;
}

/* 原有样式继续保持 */
.chat-messages {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background-color: #fafafa;
}

.message {
  display: flex;
  margin-bottom: 16px;
  max-width: 80%;
}

.message.user {
  flex-direction: row-reverse;
  margin-left: auto;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  font-size: 14px;
}

.message.user .avatar {
  margin-right: 0;
  margin-left: 8px;
  background-color: #007bff;
  color: white;
}

.content {
  padding: 8px 16px;
  border-radius: 18px;
  background-color: #e9e9eb;
  line-height: 1.5;
}

.message.user .content {
  background-color: #007bff;
  color: white;
}

.loading {
  text-align: center;
  padding: 16px;
  color: #666;
}

.chat-input {
  display: flex;
  padding: 16px;
  border-top: 1px solid #e0e0e0;
}

textarea {
  flex: 1;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 24px;
  resize: none;
  outline: none;
  font-size: 14px;
  min-height: 48px;
}

button {
  margin-left: 12px;
  padding: 0 24px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  white-space: nowrap;
}

button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
</style>